﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.StreamCollapse
{
    [Serializable]
    public class CollapseOutput:ResultBase
    {
        #region props
        private int _EntityCount;
        /// <summary></summary>
        public int EntityCount
        {
            get
            {
                return this._EntityCount;
            }
            set
            {
                this._EntityCount = value;
            }
        }

        private int _CollapseCount;
        /// <summary></summary>
        public int CollapseCount
        {
            get
            {
                return this._CollapseCount;
            }
            set
            {
                this._CollapseCount = value;
            }
        }

        private string _CollapseResultFilePath;
        /// <summary></summary>
        public string CollapseResultFilePath
        {
            get
            {
                return this._CollapseResultFilePath;
            }
            set
            {
                this._CollapseResultFilePath = value;
            }
        }

        private string _CollapseInstanceTableName;
        /// <summary></summary>
        public string CollapseInstanceTableName
        {
            get
            {
                return this._CollapseInstanceTableName;
            }
            set
            {
                this._CollapseInstanceTableName = value;
            }
        }

        private string _CollapseMappingTableName;
        /// <summary></summary>
        public string CollapseMappingTableName
        {
            get
            {
                return this._CollapseMappingTableName;
            }
            set
            {
                this._CollapseMappingTableName = value;
            }
        }
        #endregion

        #region ctor
        public CollapseOutput():base(Guid.Empty,Guid.Empty)
        {
            this._EntityCount = 0;
            this._CollapseCount = 0;
            this._CollapseResultFilePath = string.Empty;
            this._CollapseInstanceTableName = string.Empty;
            this._CollapseMappingTableName = string.Empty;
        }

        public CollapseOutput(Guid componentID, Guid jobTicketID,
            int entityCount, int collapseCount, string resultFilePath,
            string instanceTbl, string mappingTbl) 
            : base(componentID, jobTicketID)
        {
            this._EntityCount = entityCount;
            this._CollapseCount = collapseCount;
            this._CollapseResultFilePath = resultFilePath;
            this._CollapseInstanceTableName = instanceTbl;
            this._CollapseMappingTableName = mappingTbl;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CollapseOutput output = new CollapseOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.EntityCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "EntityCount", "0"));
                output.CollapseCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "CollapseCount", "0"));
                output.CollapseResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "CollapseResultFilePath", "");
                output.CollapseInstanceTableName =
                    XmlDataUtil.GetAttributeValue(xNode, "CollapseInstanceTable", "");
                output.CollapseMappingTableName =
                    XmlDataUtil.GetAttributeValue(xNode, "CollapseMappingTable", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EntityCount", this.EntityCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"CollapseCount",this.CollapseCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"CollapseResultFilePath",this.CollapseResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CollapseInstanceTable", this.CollapseInstanceTableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CollapseMappingTable", this.CollapseMappingTableName);

            return xNode;
        }
    }
}
